<template>
  <div>
    <van-nav-bar title="登陆页面" />
    <div>
      <van-form @submit="onSubmit">
      <van-field
        v-model="user.mobile"
        name="mobile"
        label="手机号"
        required
        placeholder="请填写11位手机号"
        :rules="[{ required: true, message: '请填写11位手机号', pattern :/^1[3-9]\d{9}$/}]"
      />
      <van-field
        v-model="user.code"
        type="password"
        name="code"
        label="密码"
        required
        placeholder="请填写6位密码"
        :rules="[{ required: true, message: '请填写密码',pattern :/^\d{6}$/ }]"
      />
      <div style="margin: 16px">
        <van-button
        round
        block
        :disabled = loginf
        :loading = loginf
        loading-text="加载中..."
        type="info" native-type="submit"
          >提交</van-button
        >
      </div>
    </van-form>
    </div>
  </div>
</template>

<script>
import { LoginAPI } from '@/api'
import { Toast } from 'vant'
import { setToken } from '@/utils/token'
export default {
  name: 'Login',
  data () {
    return {
      user: {
        mobile: '15253322000', // 手机号
        code: '246810' // 密码
      },
      loginf: false
    }
  },
  methods: {
    async onSubmit (values) {
      this.loginf = true
      try {
        const res = await LoginAPI(this.user)
        Toast.success('登录成功')
        setToken(res.data.data.token)
        // 登陆成功跳转
        this.$router.replace({
          path: '/layout/home'
        })
      } catch (err) {
        Toast.fail('登录失败')
      }
      this.loginf = false
    }
  }
}
</script>

<style scoped lang="less">
</style>
